package com.huike.stock_analysis.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huike.stock_analysis.entity.News;
import com.huike.stock_analysis.entity.Rank;
import com.huike.stock_analysis.service.SearchPageService;



/**
 * 
 * @author mzLEI
 *  搜索页面接口
 * 搜索功能 展示搜索页面的数据
 */
@RestController
@CrossOrigin(value = "*")
public class SearchPageController {
	@Autowired
	private SearchPageService sService;
	
	//搜索
	@RequestMapping("/search")
	public String search(String stock) {
		if(stock == null) return "null";
		System.out.println(stock);
		stock = "'" + stock + "'";
		return sService.getCode(stock);
	}

	
	/*
	//rank
	@RequestMapping("/rank")
	public String rank() {
		Map<String, String> map = sService.rank();
		ObjectMapper mapper = new ObjectMapper();
		
		String result = "";
		try {
			result = mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	*/
	//rank
	@RequestMapping("/rank")
	public String rank() {

		List<Rank> ranks = sService.rank1();
		ObjectMapper mapper = new ObjectMapper();
		
		String result = "";
		try {
			result = mapper.writeValueAsString(ranks);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	//news
	@RequestMapping("/news")
	public String news() {
		
		List<News> news = sService.news();
		ObjectMapper mapper = new ObjectMapper();
		
		String result = "";
		try {
			result = mapper.writeValueAsString(news);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
